/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabasePack;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author abdosalah
 */
public class BankDatabase {

    public boolean login(int IDCard, int password) {
        long temp = 0;//if temp = 0 password wronge else password true
        if (sqlstatments.Check_IdCard(IDCard) == true) {
            try {
                ResultSet re =sqlstatments.SelectWithCondition("ID_CARD", IDCard);
                re.next();
                int pass = re.getInt("password");

                if (password == pass) {
                    temp++;
                }
            } catch (Exception e) {
                System.out.println("login fun");
            }
        } else {
            return false;
        }

        if (temp == 1) {
            return true;
        } else {
            return false;
        }
    }

    public String Withdraw(int idCard, int money) {
        int modify_Account;
        
        try {
            ResultSet client =sqlstatments.select_particular_client_by_IdCard(idCard);
            int client_Account = client.getInt("MONEY");
            if (client_Account > money) {
                modify_Account = client_Account - money;
                if (sqlstatments.update_Account_Money(idCard, modify_Account)) {
                    return "" + modify_Account;
                } else {
                    return "0";
                }
            }//end of id Account > money
            else {
                return "0";
            }//end of else Account < money
        }//end of try
        catch (Exception e) {
            System.out.println(e.getMessage());
            return "0";
        }//end of catch

    }

    public String Deposit(int idCard, int money) {
        int modify_Account;
        try {
            ResultSet client =sqlstatments.select_particular_client_by_IdCard(idCard);
            int client_Account = client.getInt("MONEY");

            modify_Account = client_Account + money;
            if (sqlstatments.update_Account_Money(idCard, modify_Account)) {
                return "" + modify_Account;
            } else {
                return "0";
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "0";
        }

    }
  
    public String transfer(int account_nu, int money, int card_id) {
        String card_id_res =sqlstatments.Check_Account_nu(account_nu);
        if (!card_id_res.equals("false")) {
            //0   1 
            String re = Withdraw(card_id, money);
            if (!re.equals("0")) {
                if (Deposit(Integer.parseInt(card_id_res), money) != "0") {
                    return re;
                } else {
                    return "3";
                }
            } else {
                return "0";
            }
        } else {
            return "1";
        }
    }

    public String bill_payment(int card_id, String company_name, int money, int bill_nu, int phone_nu) {
        try {
            if (Withdraw(card_id, money) != "0") {
                ResultSet client;
                client =sqlstatments.SelectWithCondition("ID_CARD", card_id);
                client.next();
               /* ResultSet company;
                company =sqlstatments.Select_with_string_condition("COMPANY", "NAME", company_name);
                int update_company_money = company.getInt("BALABNCE");
                update_company_money = update_company_money + money;
                sqlstatments.update_one_sritng_col_with_int_condition("COMPANY", "BALABNCE", "" + update_company_money, "ID", "" + company.getInt("ID"));*/
                sqlstatments.insert_into_billbay_transaction(company.Update_Company_money(company_name, money), client.getInt("CLIENT_ID"), phone_nu, money, bill_nu);
                return "" + client.getInt("MONEY");
            } else {
                return "0";
            }
        } catch (Exception e) {
            System.out.println("bill_payment function");
            return "dbERROR";
        }
    }

    public String Change_PIN(int id_card, int new_PIN) {
        boolean result;

        result =sqlstatments.update_one_sritng_col_with_int_condition("CLIENT", "PASSWORD", "" + new_PIN, "ID_CARD", "" + id_card);
        if (result) {
            return "" + new_PIN;
        } else {
            return "0";
        }
    }

    public String Balancing_query(int card_id) {
        //
        ResultSet re;
        re =sqlstatments.SelectWithCondition("ID_CARD", card_id);
        try {
            re.next();
            return "" + re.getInt("MONEY");
        } catch (Exception e) {
            System.out.println("Balancing_query problem");
            return "dberror";
        }
    }
}
